﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using FILETIME = System.Runtime.InteropServices.ComTypes.FILETIME;

namespace CabConfigProvisioning
{
    static class FxiHelpers
    {
        public static Stream StreamFromHandle(IntPtr hf)
        {
            return (Stream)((GCHandle)hf).Target;
        }

        public static void SplitFilePath(FileInfo file, out string sDirectory, out string sFileName)
        {
            string sDir = file.DirectoryName;
            if (!string.IsNullOrEmpty(sDir))
                sDir += Path.DirectorySeparatorChar;
            sDirectory = sDir;
            sFileName = file.Name;
        }

        [DllImport("kernel32.dll")]
        public static extern bool FileTimeToDosDateTime([In] ref FILETIME lpFileTime, out ushort lpFatDate, out ushort lpFatTime);

        public static void GetDosTime(DateTime time, out ushort dosDate, out ushort dosTime)
        {
            var fileTime = BitConverter.GetBytes(time.ToFileTime());
            var ft = new FILETIME
            {
                dwLowDateTime = BitConverter.ToInt32(fileTime, 0),
                dwHighDateTime = BitConverter.ToInt32(fileTime, 4)
            };
            FileTimeToDosDateTime(ref ft, out dosDate, out dosTime);
        }

    }
}
